package chu.dejian.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import java.util.ArrayList;
import java.util.List;

import chu.dejian.entity.AttendeeEntity;
import chu.dejian.entity.ContactEntity;
import chu.dejian.entity.MeasureMessageEntity;
import chu.dejian.entity.RecordEntity;
import chu.dejian.widget.AttentionLevelView;

public class AttendeesDao {

	public static final String ATTENDEE_TABLE = "attendees";

	public static final String KEY_AT_ID = "atid";
	public static final String KEY_ATTENDEE = "attendee";
	public static final String KEY_ATTENDEE_NAME = "attendee_name";
	public static final String KEY_ATTENDER_LEVEL = "attender_level";
	public static final String KEY_ATTENDEE_LEVEL = "attendee_level";

	public static final String ATTENDEE_TABLE_CREATE = "CREATE TABLE "
			+ ATTENDEE_TABLE + " ("
			+ KEY_AT_ID + " INTEGER PRIMARY KEY, "
			+ KEY_ATTENDEE + " VARCHAR, "
			+ KEY_ATTENDEE_NAME + " VARCHAR, "
			+ KEY_ATTENDER_LEVEL + " TINYINT, "
			+ KEY_ATTENDEE_LEVEL + " TINYINT);";

	public long insertAttendee(AttendeeEntity attendeeEntity) {
		if(getAttendee(attendeeEntity.attendeeId) != null) return -1;
		return DBHelper.getInstance().getDatabase().insert(ATTENDEE_TABLE, null, createContentValues(attendeeEntity));
	}

	public boolean updateAttendee(AttendeeEntity attendeeEntity) {
		return DBHelper.getInstance().getDatabase().update(ATTENDEE_TABLE, createContentValues(attendeeEntity), KEY_AT_ID + "=" + attendeeEntity.atid, null) > 0;
	}

	public boolean deleteAttendee(String attendeeId) {
		return DBHelper.getInstance().getDatabase().delete(ATTENDEE_TABLE, KEY_AT_ID + "=" + attendeeId, null) > 0;
	}

	public List<AttendeeEntity> getAllAttendee() {
		Cursor cursor = DBHelper.getInstance().getDatabase().query(ATTENDEE_TABLE,null,	null, null, null, null, null);
		List<AttendeeEntity> attendeeList = new ArrayList<>();
		while (cursor != null && cursor.moveToNext()) {
			attendeeList.add(getAttendeeByCursor(cursor));
		}
		cursor.close();
		return attendeeList;
	}

	public AttendeeEntity getAttendeeByCursor(Cursor cursor){
		AttendeeEntity attendeeEntity = new AttendeeEntity();
		attendeeEntity.atid = cursor.getInt(cursor.getColumnIndex(KEY_AT_ID));
		attendeeEntity.attendeeId = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE));
		attendeeEntity.attendeeName = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE_NAME));
		attendeeEntity.attendeeLevel = AttentionLevelView.ATTENTION_LEVEL.fromInt(cursor.getInt(cursor.getColumnIndex(KEY_ATTENDEE_LEVEL)));
		attendeeEntity.attenderLevel = AttentionLevelView.ATTENTION_LEVEL.fromInt(cursor.getInt(cursor.getColumnIndex(KEY_ATTENDER_LEVEL)));
		return attendeeEntity;
	}

	public AttendeeEntity getAttendee(String attendeeId) throws SQLException {
		Cursor cursor = DBHelper.getInstance().getDatabase().rawQuery("SELECT * FROM "+ ATTENDEE_TABLE + " WHERE "+ KEY_AT_ID +" = ?;",new String[]{attendeeId});
		if (cursor != null && cursor.moveToFirst()) {
			return getAttendeeByCursor(cursor);
		}
		return null;
	}

	private ContentValues createContentValues(AttendeeEntity attendeeEntity) {
		ContentValues values = new ContentValues();
		values.put(KEY_AT_ID,attendeeEntity.atid);
		values.put(KEY_ATTENDEE,attendeeEntity.attendeeId);
		values.put(KEY_ATTENDEE_NAME,attendeeEntity.attendeeName);
		values.put(KEY_ATTENDER_LEVEL,attendeeEntity.attenderLevel.value());
		values.put(KEY_ATTENDEE_LEVEL,attendeeEntity.attendeeLevel.value());
		return values;
	}
}
